package com.loongson;


import java.util.ArrayList;
import java.util.Iterator;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPoolResource {
	private static Logger logger = LoggerFactory.getLogger(RedisPoolResource.class);
	public static JedisPool redisPool ;
	public static RedisPoolResource instance = new RedisPoolResource();
	

	private  RedisPoolResource(){	
		logger.info("Construct function");
//		BufferedReader bufferedReader;
//		try {
//			bufferedReader = new BufferedReader(new FileReader("redis.properties"));
//			Properties properties = new Properties();
//			properties.load(bufferedReader);
//			String host =  properties.getProperty("HOST");
//			int port = Integer.parseInt(properties.getProperty("PORT"));
//			int maxTotal = Integer.parseInt(properties.getProperty("MAXTOTAL"));
			String host =  ConstProperties.HOST;
			int port = Integer.parseInt(ConstProperties.PORT);
			int maxTotal = Integer.parseInt(ConstProperties.MAXTOTAL);
			logger.info("Redis config: host:{} ,port:{},max total:{}",host,port,maxTotal);
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(maxTotal);
			redisPool = new JedisPool(config, host, port,2000);
			
//			bufferedReader.close();
//		} catch (FileNotFoundException e) {
//			logger.error(new File("redis.properties").getAbsolutePath());
//			logger.error(e.getMessage());
//		} catch (IOException e) {
//			logger.error(e.getMessage());
//		}
			
	};

	public static synchronized Jedis getRedis(){
//		logger.info("Get redis instance");
		Jedis redis =  redisPool.getResource();
		logger.info("redis pool active left:{}",redisPool.getNumActive());
		return redis;
		
	}
	public static RedisPoolResource getInstance(){
		if(instance == null){
			instance = new RedisPoolResource();
		}
		return instance;
	}
	public void lpushVmInfo(String value){
		logger.info("push vm info");
		Jedis jedis = RedisPoolResource.getRedis();
		jedis.lpush(ConstProperties.REDISVMINFOLIST, value);
		jedis.close();
		
	}
	public void lpushVmList(ArrayList<VmInfo> vmInfoList){
		logger.info("push vm info to redis list");
		Jedis redis = RedisPoolResource.getRedis();
		for (Iterator<VmInfo> iterator = vmInfoList.iterator(); iterator.hasNext();) {
			VmInfo vmInfo = (VmInfo) iterator.next();	
			redis.lpush(ConstProperties.REDISVMINFOLIST, vmInfo.toJsonString());
			long initalDelay = UtilTool.getDelay(23,59,59);
			redis.expire(ConstProperties.REDISVMINFOLIST, initalDelay);
			
		}
		redis.close();
		
	}
	public String lPopVmInfo(){
		logger.info("get vm info");
		String value = null;
		Jedis jedis = RedisPoolResource.getRedis();
		value = jedis.lpop(ConstProperties.REDISVMINFOLIST);
		System.out.println(value);
		jedis.close();
		return value;
	}
	public void AddUser(String user,String pass){
		logger.info("insert user");
		Jedis jedis = RedisPoolResource.getRedis();
		jedis.sadd(ConstProperties.REDISUSE, user);
		jedis.close();		
	}
	public boolean CheckUserExist(String user){
		logger.info("check user exist");
		Jedis jedis = RedisPoolResource.getRedis();
		boolean exist = jedis.sismember(ConstProperties.REDISUSE, user);
		logger.info("exist");
		jedis.close();	
		return exist;
	}
	public String AddRelationship(String user){
		logger.info("get vnc for user");
		if(! CheckUserExist(user)){
			return null;
		}
		String vmStr = lPopVmInfo();
		if(vmStr == null || vmStr == ""){
			return null;
		}
		VmInfo vm = UtilTool.GetVmInfoFromJson(vmStr);
		Jedis jedis = RedisPoolResource.getRedis();
		jedis.set(user, vm.getVncUrl());
		jedis.close();
		return vm.getVncUrl();

	}
	public String getMyVncUrl(String user){
		logger.info("get vnc of user");
		String vncUrl = null;
		logger.info("check if user has  vnc ");
		Jedis jedis = RedisPoolResource.getRedis();
		vncUrl = jedis.get(user);
		logger.info("vnc url of user:{} ", vncUrl);
		if(vncUrl == null){
			vncUrl ="";
		}
		jedis.close();
		return vncUrl;
	}
	public long getTotalUrl(){
		logger.info("get total number of vnc");

		Jedis jedis = RedisPoolResource.getRedis();
		long num = jedis.llen(ConstProperties.REDISVMINFOLIST);
		logger.info("get total number of vnc :{}",num);
		jedis.close();
		return num;
		
		
	}
	public static void main(String[] args){

		// String mail = "123@qq.com";
		// String pass = "33";
	
//		RedisPoolResource instance1 = RedisPoolResource.getInstance();
		Jedis jedis = RedisPoolResource.getRedis();
		for(int i=0;i<100;i++){
			String serverId = "1234567890";
			String vncUrl = "http://61.153.251.58:10417/vnc_auto.html?token=6355f3f1-4f39-453e-994e-7e70b64fe7f4";
			String name = "pub1";
			String floatIp = "192.168.0.1";
			jedis.lpush(ConstProperties.REDISVMINFOLIST, new VmInfo(serverId,vncUrl,name,floatIp).toJsonString());
		}
		jedis.close();
	

	}



}
